// JavaScript Document
var p = new Player();

//Editor
editor = {
	tool: "w1"
}

//Camera
camera = {
	scale: 1,
	x: 0,
	y: 0
}
//Mouse
mouse = {
	x: 0,
	y: 0,
	press: false
	
}
//Level
var mapFiles = new Array();
level = {
	name: "Error 100 - Level not found",
	map:[,],
	spawnPos: {
		"x":0,
 		"y":0
 	}
}

//Init keys

function mouseOutOfCanvas(evt) {
	mouse.press = false;
}
function mouseMove(evt) {
	var obj = canvas;
    var top = 0;
    var left = 0;
    while (obj && obj.tagName != 'BODY') {
        top += obj.offsetTop;
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    // return relative mouse position
    mouse.x = evt.clientX - left + window.pageXOffset;
    mouse.y = evt.clientY - top + window.pageYOffset;
}
function onClick(evt) {
	
}
function mouseUp(evt) {
	mouse.press = false;
}
function mouseDown(evt) {
	mouse.press = true;
}

window.onload = init;

function initListeners() {
	canvas.addEventListener ("mouseout", mouseOutOfCanvas, false);
	canvas.addEventListener("mousemove", mouseMove, false);
	canvas.addEventListener("onclick", onClick, false);
	canvas.addEventListener("mouseup", mouseUp, false);
	canvas.addEventListener("mousedown", mouseDown, false);
}

function update() {
	p.update();
	updateArrowKeys();
	updateKeys();
	updateMap();
	updateCamera();
}

function mouseGrid() { 
	return {
        x: Math.floor((mouse.x - camera.x) / grid),
        y: Math.floor((mouse.y - camera.y) / grid)
    };
}

function updateMap() {
	if (mouse.press) {
		var mouseGridPos = mouseGrid();
		//alert(level.map[mouseGridPos.x][mouseGridPos.y]);
		level.map[mouseGridPos.x][mouseGridPos.y] = editor.tool;
	}
}

function updateCamera() {
	ctx.setTransform(1, 0, 0, 1, 0, 0);
	//camera.x = (-p.x - grid / 2) * camera.scale + canvas.width/2;
	//camera.y = (-p.y - grid / 2) * camera.scale + canvas.height/2;
	camera.x = 300;
	camera.y = 300,
	ctx.translate(
		camera.x,
		camera.y);
	ctx.scale(camera.scale, camera.scale);
}

function updateArrowKeys() {
	if (17 in keys && keys[17]) { //ctrl
		alert("CTRL");
		canvasLostFocus();
	}
	if (38 in keys && keys[38] || (87 in keys && keys[87])) { //up
		if (!p.isMoving()) {
			if (isPlaceFree(p.getPosX(), p.getPosY() - 1)) {
				p.moveY(-1);
			}
		}
	}
	if ((39 in keys && keys[39]) || (68 in keys && keys[68])) { //right
		if (!p.isMoving()) {
			if (isPlaceFree(p.getPosX() + 1, p.getPosY())) {
				p.moveX(1);
			}
		}
	}
	if ((40 in keys && keys[40]) || (83 in keys && keys[83])) { //down
		if (!p.isMoving()) {
			if (isPlaceFree(p.getPosX(), p.getPosY() + 1)) {
				p.moveY(1);
			}
		}
	}
	if (37 in keys && keys[37] || (65 in keys && keys[65])) { //left
		if (!p.isMoving()) {
			//alert(isPlaceFree(p.getPosX(), p.getPosY()));
			if (isPlaceFree(p.getPosX() - 1, p.getPosY())) {
				p.moveX(-1);
			}
		}
	}
}


function updateKeys() {
	if ((78) in keys) { //n
		if (!keylock[78]) {
			var answer = confirm("You want to create a new map?");
			if (answer) {}
				
			}
			keylock[78] = true;
		}
}

function draw() {
	//ctx.drawImage(images["TilesSprite2"], 0, 0);
	drawWorld();
	
	p.draw();
	
	/*
	drawGrid();
	drawHud();*/
}

function drawGrid() {
	//selected grid
	//ctx.fillStyle = "rgba(244, 130, 30, 0.5)"; 
	ctx.fillStyle = "rgba(100, 255, 255, 0.5)"; 
	var mouseGridPos = mouseGrid();
	ctx.fillRect(mouseGridPos.x * grid,mouseGridPos.y * grid,grid,grid);
	
	ctx.strokeStyle = "rgba(0, 0, 0, 0.5)"; 
	//horisontaalne
	for (i = 0; i < (canvas.width / grid); i++) {
		var j = i * grid - Math.round(camera.x / grid) * grid;
		ctx.moveTo(j, -camera.y);
		ctx.lineTo(j, canvas.height);
	}
	for (i = 0; i < (canvas.height / grid); i++) {
		var j = i * grid - Math.round(camera.y / grid) * grid;
		ctx.moveTo(-camera.y, j);
		ctx.lineTo(canvas.height, j);
	}
	ctx.stroke();
}

function drawWorld() {
	for (i = 0; i < 10; i++) {//level.map.lenght;
		for (j = 0; j < 10; j++) {
			var t = level.map[i][j];
			var tnum = parseInt(t.substr(1, 2));
			switch (t.charAt(0)) {
				case 'f':
				drawGridObject(images["TilesSprite2"], tnum, 1, i * grid, j * grid);
				break;
				case 'w':
				drawGridObject(images["TilesSprite2"], tnum, 2, i * grid, j * grid);
				break;
			}
		}
	}
}

function drawGridObject(spriteSheet,sx, sy, x, y) {
	try {  
    	ctx.drawImage(spriteSheet, sx * grid, sy * grid, grid, grid, 
    	x, y, grid, grid);
    } catch(e) {alert(e)}
}

function isPlaceFree(x, y) {
	var t = level.map[x][y];
	if (t.length > 0) {
		switch (t.charAt(0)) {
			case 'w':
			return false;
			break;
		}
	}
	return true;
}

function drawHud() {
	ctx.save();
	ctx.setTransform(1, 0, 0, 1, 0, 0);
	
	//FPS
	ctx.fillStyle = "#FFF";
	ctx.font = "bold 12px sans-serif";
  	ctx.fillText("fps: " + Math.min(curFPS,FPSlimit) + "/" + FPSlimit, 10, 20);
  	++numFramesDrawn;
	
	ctx.restore();
}
